<?php
/*
 * Created on 26/06/2014 by lion
 *
 */
class Geocoder{
    static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=";

    static public function getLocation($address){
        $url = self::$url.urlencode($address);
        
        $resp_json = self::curl_file_get_contents($url);
        $resp = json_decode($resp_json, true);

        if($resp['status']='OK'){
        	if($resp['results']!=null){
            return $resp['results'][0]['geometry']['location'];}
        }else{
            return false;
        }
    }


    static private function curl_file_get_contents($URL){
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
            else return FALSE;
    }
} 

/** how to use this lib
 * 
 * $address=urlencode("1600 Amphitheatre Parkway, Mountain View, CA");
 * $loc = geocoder::getLocation($address);
 * 
 */
?>
